package com.guojian.http.handle;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import com.guojian.http.handle.service.HttpJettyBeforeHandle;

/**
 * Jetty_ 请求解析,响应处理类
 * @author guoJian
 *
 */
public abstract class AbstractJettyHandler extends AbstractHandler {

	private HttpJettyBeforeHandle beforeHandle = new HttpJettyBeforeHandle();

	@Override
	final public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		if (response.isCommitted())
			throw new RuntimeException("response is committed, could not continue handle request ");

		if (!baseRequest.isHandled())
			baseRequest.setHandled(true); //handled is false, will be 404

		beforeHandle.handleRequest(baseRequest, response);

		handleJettyRequestResponse(target, baseRequest, request, response);

	}

	protected abstract void handleJettyRequestResponse(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;

}
